<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="javax.persistence.EntityTransaction,javax.persistence.EntityManagerFactory,javax.persistence.EntityManager" %>
<%@ page import="treqs.model.HibernateUtil,treqs.model.Requirement,treqs.model.Requirementpriority,treqs.model.Requirementstatus"%>
<%@ page import="java.util.List, java.util.Iterator" %>

<%
Requirement requirement = null;
Boolean createNewRequirement = false;
try
{			
	EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
	EntityManager em = emf.createEntityManager();
	EntityTransaction tx = em.getTransaction();
	tx.begin();

	if (null != request.getParameter("id"))
	{		
		int requirementID = Integer.parseInt(request.getParameter("id"));
		requirement = em.find(Requirement.class, requirementID);
	}
	if (null == requirement)
	{		
		createNewRequirement = true;
		requirement = new Requirement();
        requirement.setUid("someUid");
        requirement.setName("someName");
        requirement.setDescription("someDescription");
        requirement.setRationale("someRationale");
        requirement.setComment("someComment");
        requirement.setRevision(1);
		requirement.setRequirementpriority(em.find(Requirementpriority.class, 1));
		requirement.setRequirementstatus(em.find(Requirementstatus.class, 1));
	}

	tx.commit();
	em.close();
}
catch (Exception ex)
{
	out.println(ex.toString());
}
%>

    <form action="/treqs/RequirementManageServlet" method="post">
        <fieldset>
            <legend>Fields</legend>
            <%
            if (!createNewRequirement)
            {
            	out.println("<input type=\"hidden\" name=\"id\" value=\"" + requirement.getId() + "\">");
            }
            %>
            <p>
                <label for="UID">UID:</label>
                <input id="UID" name="UID" type="text" value="<%= requirement.getUid() %>" />
            </p>
            <p>
                <label for="name">name:</label>
                <input id="name" name="name" type="text" value="<%= requirement.getName() %>" />
            </p>
            <p>
                <label for="description">description:</label>
                <textarea id="description" name="description"><%= requirement.getDescription() %></textarea>
            </p>
            <p>
                <label for="rationale">rationale:</label>
                <textarea id="rationale" name="rationale"><%= requirement.getRationale() %></textarea>
            </p>
            <p>
                <label for="comment">comment:</label>
                <textarea id="comment" name="comment"><%= requirement.getComment() %></textarea>
            </p>

            <p>
				<label for="priority">priority:</label>
				<select id="priority" name="priority">
				<%
					try
					{			
						EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
						EntityManager em = emf.createEntityManager();
						EntityTransaction tx = em.getTransaction();
						tx.begin();
				
						List requirementPriorities = 
							em.createQuery(
							"select requirementpriority from Requirementpriority as requirementpriority")
					    .getResultList();
						Requirementpriority requirementPriority;
						for(Iterator itr = requirementPriorities.iterator(); itr.hasNext();)
						{
							requirementPriority = (Requirementpriority)itr.next();
							out.println("<option " 
									+ (requirementPriority.getId().equals(requirement.getRequirementpriority().getId()) 
											? "selected " : "") 
									+ "value=\"" + requirementPriority.getId() + "\">" + requirementPriority.getValue() + "</option>");
						}
						tx.commit();
						em.close();
					}
					catch (Exception ex)
					{
						out.println(ex.toString());
					}
				%>
				</select>
            </p>
            <p>
				<label for="status">status:</label>
				<select id="status" name="status">
				<%
					try
					{			
						EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
						EntityManager em = emf.createEntityManager();
						EntityTransaction tx = em.getTransaction();
						tx.begin();
				
						List requirementStatuses = 
							em.createQuery(
							"select requirementstatus from Requirementstatus as requirementstatus")
					    .getResultList();
						Requirementstatus requirementStatus;
						for(Iterator itr = requirementStatuses.iterator(); itr.hasNext();)
						{
							requirementStatus = (Requirementstatus)itr.next();
							out.println("<option " 
									+ (requirementStatus.getId().equals(requirement.getRequirementstatus().getId()) 
											? "selected " : "") 
									+ "value=\"" + requirementStatus.getId() + "\">" + requirementStatus.getValue() + "</option>");
						}
						tx.commit();
						em.close();
					}
					catch (Exception ex)
					{
						out.println(ex.toString());
					}
				%>
				</select>
            </p>
            <p>
                <input type="submit" value="<%= (createNewRequirement ? "Create" : "Modify") %>" />
            </p>
        </fieldset>

    </form>